/**
 *emall js api
 *
 **/
var EMALL = (function () {
    var what = {};
    var IE = /msie/i.test(navigator.userAgent);//判断是否为ie浏览器

    //判断是否为数组
    function isArray(o) {
        return Object.prototype.toString.call(o) === "[object Array]";
    }

    //动态加载js文件
    function loadScript(sURL, oCallBack) {
        var js;
        js = document.createElement("script");
        js.charset = "UTF-8";
        if (IE) {
            js.onreadystatechange = function () {
                if (js.readyState.toLowerCase() == "complete" || js.readyState.toLowerCase() == "loaded") {
                    oCallBack.call(oCallBack);
                }
            };
        }
        else {
            js.onload = function () {
                oCallBack.call(oCallBack);
            };
        }
        js.src = sURL;
        document.getElementsByTagName("head")[0].appendChild(js);
    }

    what.regist = function (sNameSpace, oMark) {
        var currentPart;
        var rootObject = what;
        var namespaceParts = sNameSpace.split(".");
        for (var i = 0; i < namespaceParts.length; i++) {
            currentPart = namespaceParts[i];
            if (rootObject[currentPart] == null) {
                if (i == namespaceParts.length - 1 && oMark != null) {
                    rootObject[currentPart] = oMark;
                }
                else {
                    rootObject[currentPart] = {};
                }
            }
            rootObject = rootObject[currentPart];
        }
        return rootObject;
    };
    what.regist("core", {
        load:function (aPkgName, oCallBack) {
            var arr = isArray(aPkgName) ? aPkgName : [aPkgName];
            var final_libs, core_libs;
            if (window.loaded_emall_core_libs == undefined) {
                final_libs = core_libs = ['defaults', 'api_url', 'extend', 'api_ajax'];
                window.loaded_emall_core_libs = true;
            }
            else {
                final_libs = core_libs = [];
            }

            var i, len = arr.length,
                key, loadedNum = 0,
                final_len = core_libs.length,
                load_module;
            if (window.jQuery == undefined) {
                final_libs[final_len++] = 'jquery';
            }

            for (i = 0; i < len; i++) {
                final_libs[final_len++] = arr[i];
            }

            for (i = 0; i < final_len; i++) {
                key = final_libs[i];
                load_module = moduleList[key];

                loadScript(load_module,
                    function () {
                        loadedNum++;
                        if (loadedNum == final_len) {
                            oCallBack.call(oCallBack);
                        }
                    });
            }
        }
    });
    return what;
})();